﻿using System.Windows;
using System.Windows.Controls;
using XmlStudio.ViewModel.Node;
using System.Windows.Input;
using XmlStudio.Dialogs;

namespace XmlStudio.Resources.UserControls {
    /// <summary>
    /// Interaction logic for UcAttributes.xaml
    /// </summary>
    public partial class UcAttributes : UserControl {
        public static RoutedCommand RemoveAttribute = new RoutedCommand();

        public UcAttributes() {
            InitializeComponent();

            this.CommandBindings.Add(new CommandBinding(UcAttributes.RemoveAttribute, RemoveAttributeExecuted));
        }

        protected void RemoveAttributeExecuted(object sender, ExecutedRoutedEventArgs e) {
            if(e.Parameter is XmlAttributeViewModel) {
                this.Attributes.Remove(e.Parameter as XmlAttributeViewModel);
            }
        }

        public static readonly DependencyProperty AttributesProperty =
            DependencyProperty.Register("Attributes", typeof(XmlAttributeCollectionViewModel), typeof(UcAttributes), new UIPropertyMetadata(new XmlAttributeCollectionViewModel()));

        public static readonly DependencyProperty ItemTemplateProperty =
            DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(UcAttributes), new UIPropertyMetadata());

        public XmlAttributeCollectionViewModel Attributes {
            get { return (XmlAttributeCollectionViewModel)GetValue(AttributesProperty); }
            set { SetValue(AttributesProperty, value); }
        }

        public DataTemplate ItemTemplate {
            get { return (DataTemplate)GetValue(ItemTemplateProperty); }
            set { SetValue(ItemTemplateProperty, value); }
        }

        public static readonly DependencyProperty ReadOnlyProperty =
            DependencyProperty.Register("ReadOnly", typeof(bool), typeof(UcAttributes), new UIPropertyMetadata());

        public bool ReadOnly {
            get { return (bool)GetValue(ReadOnlyProperty); }
            set { SetValue(ReadOnlyProperty, value); }
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            AddAttributeDialog dialog = new AddAttributeDialog();

            Point pos = Mouse.GetPosition(this);
            Point pos2 = PointToScreen(pos);
            Point mousePosition = pos2;
            dialog.Left = mousePosition.X;
            dialog.Top = mousePosition.Y;

            bool? res = dialog.ShowDialog();
            if(res.HasValue && res.Value) {
                XmlAttributeViewModel attribute = dialog.Attribute;
                this.Attributes.Add(attribute);
            }
        }
    }
}